var util = require('../../../utils/util')
var api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    taskassigment: {},
    //人员列表
    taskAssigmentUsers:[],
    //选择人员
    taskAssigmentUser:null,
    //是否展示
    showuser:false
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('row', function (data) {
      //获取任务详情
      that.setData({
        taskassigment: data,
        taskAssigmentUsers:data.taskAssigmentUsers
      })

    })

  },

  //分配
  selectUser(event) {
    let row = event.currentTarget.dataset.data;
    console.log(row)
    this.hideModal()
    this.setData({
      taskAssigmentUser:row,
      showuser:true
    })
    



  },

  //进度条
  loadModal() {
    this.setData({
      loadModal: true
    })
  },
  closeModal() {
    this.setData({
      loadModal: false
    })
  },
  //人员选择模态框
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },

  //保存提交
  save() {

    if (this.data.taskAssigmentUser == '' || this.data.taskAssigmentUser == null) {
      util.showErrorToast('请选择人员！');
      return false
    }

    //提交上报数据
    let dataup = {
      taskId: this.data.taskassigment.task.taskId,
      appletUserId:this.data.taskAssigmentUser.appletUserId
    };
    let that = this;
    that.loadModal();

    util.request(api.assignmentUser, JSON.stringify(dataup), 'POST').then(function (res) {
      console.log(res)
      if (res.code == '200') {
        that.closeModal();
        //刷新界面
        console.log("========")
        wx.navigateBack()

      } else {
        util.showErrorToast(res.message);
        that.closeModal();
      }

    }).catch((err) => {
      that.closeModal();
      console.log("登录远程服务器catch:" + err);
      //reject(err);
    });


  },


})